package ro.gateway.aida.utils;

import java.lang.reflect.Method;



/**
 * <p>Title: Local Projects Database</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro; Mihai Postelnicu mihai@ro-gateway.org
 * @version ObjectFinder.java,v 1.3 2003/12/28 15:15:12 mihai Exp $
 * @version 1.2 - added generic getter
 *
 */
public class ObjectFinder
{

      //list of getters for this object
      public static String[] getters =
            {"getAttribute", "get", "getObject", "findObject"};


      public static Object findObject(
            String name,
            Object where_to_lookup,
            String class_name )
      {

            if ( where_to_lookup == null )
                  return null;

            Object[] para = new Object[]{name};
            Object found_object = null;
            Method getMethod = null;
            for ( int i = 0;
                  i < getters.length;
                  i++, getMethod = null, found_object = null )
            {
                  try
                  {
                        getMethod =
                              where_to_lookup.getClass().getMethod(
                                    getters[ i ],
                                    new Class[]{String.class} );
                  }
                  catch ( Exception e )
                  {
                  }

                  if ( getMethod != null )
                  {
                        try
                        {
                              found_object = getMethod.invoke( where_to_lookup, para );
                        }
                        catch ( Exception e )
                        {
                        }
                  }
                  if ( found_object != null ) break;
            }

            if ( found_object != null && class_name != null )
            {
                  if ( !class_name.equals( found_object.getClass().getName() ) )
                        found_object = null;
            }

            return found_object;
      }

      public static void main( String[] agrs )
      {
            System.out.println( String.class.getName() );
      }
}
